<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

class Sociallogin_Component_Controller_Result extends Phpfox_Component {

    public function process() {
        if (isset($_POST['contact']) && $_POST['contact'] != null) {
            var_dump($_POST);die;
            $url = Phpfox::getLib("url")->makeUrl('');
            $query = urldecode($_POST['contact']);
            $aList = explode("&", $query);
            $aVals = array();
            foreach ($aList as $List) {
                $value = explode("=", $List);
                if (count($value) == 2) {
                    $aVals[$value[0]] = $value[1];
                }
            }
            $aVals['password'] = substr(time(), 0, 6);
            $aVals['full_name'] = isset($aVals['name'])?$aVals['name']:$aVals['nickname'];
            $aVals['identity'] = isset($aVals['id'])?$aVals['id']:$aVals['guid'];
            
            $aVals['service_id'] = '0';
            
            if (isset($_POST['service']) && $_POST['service'] != null) {
                $aVals['service_id'] = $_POST['service'];
            }

            if (isset($_POST['service'])) {
                $aVals['service_id'] = $_POST['service'];
            }

            /* if(isset($aVals['email']))
              {
              $aUserPhpfox = Phpfox::getService("sociallogin")->getInfoUserPhpfox($aVals['email']);
              if($aUserPhpfox)
              {
              unset($aVals['email']);
              }
              } */

            if ($aVals['service_id'] != 0) {

                $aUser = Phpfox::getService("sociallogin")->getAgentByIdentityAndService($aVals['identity'], $aVals['service_id']);
                
                if (!$aUser) {
                    if (!isset($aVals['email'])) {
                        $this->template()->assign(array(
                            'contact' => $_POST['contact'],
                            'service' => $aVals['service_id'],
                        ));
                        if (isset($_POST['email']) && $_POST['email'] != "") {
                            $is_not_error = Phpfox::getService('sociallogin')->checkemail($_POST['email']);
                            if (!$is_not_error)
                                return;
                            $aVals['email'] = $_POST['email'];
                        }
                        else {
                            return;
                        }
                    }
                    $aUserPhpfox = Phpfox::getService("sociallogin")->getInfoUserPhpfox($aVals['email']);
                    if ($aUserPhpfox) {
                        Phpfox::getService('sociallogin')->addAgent($aUserPhpfox['user_id'], $aVals);
                        list($bLoginOK, $aUserLogin) = Phpfox::getService('user.auth')->login($aVals['email'], 'password', TRUE, 'email', TRUE);
                    } else {
                        var_dump($aVals);
                        if ($iId = Phpfox::getService('sociallogin')->addUser($aVals)) {                            
                            Phpfox::getService('sociallogin')->addAgent($iId, $aVals);
                            if (Phpfox::getService('user.auth')->login($aVals['email'], $aVals['password'])) {
                                $this->url()->send('');
                            }
                            $this->url()->send('user.login');
                        }
                        else{
                            Phpfox_Error::set("Email Existed. Please use another one");
                        }
                    }
                } else {
                    // signature: login($sLogin, $sPassword, $bRemember = false, $sType = 'email', $bNoPasswordCheck = false)
                    list($bLoginOK, $aUserLogin) = Phpfox::getService('user.auth')->login($aUser['email'], 'password', TRUE, 'email', TRUE);
                }
            }
            /*
            echo '
                <script type="text/javascript">
                    var url = "' . $url . '";
                    opener.location.href = url;
                    self.close();
                </script>
                ';*/
        }
    }

}

?>